home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5828 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  97 lines

  1. Path: news.win.tue.nl!not-for-mail
  2. From: exspectf@wsinis12.win.tue.nl (Richard Braeken)
  3. Newsgroups: comp.lang.c
  4. Subject: Serial port Sparc Station -> C program
  5. Date: 21 Feb 1996 17:30:25 +0100
  6. Organization: Eindhoven University of Technology, the Netherlands
  7. Message-ID: <4gfhb1$l3e@wsinis12.win.tue.nl>
  8. NNTP-Posting-Host: wsinis12.win.tue.nl
  9.  
  10. Hello,
  11.  
  12. I am working on a project for my university which has to do with a
  13. digital model railroad. Instead of using a control unit I use my Sparc
  14. Station, a serial port and a C program. To change the speed of the loc
  15. I have to send a number through this port. This works the way it
  16. should.
  17.  
  18. However when I read from the port I have a problem. The program hangs. I
  19. think I have used a wrong set up, but after a day of reading the manuals
  20. I still have no idea what's wrong. 
  21.  
  22. I have attached the program to this posting. Is there anybody in this
  23. newsgroup who has some knowledge of serial ports of Sparc Stations.
  24. Your help is greatly appreciated.
  25.  
  26. Thanks in advance,
  27.  
  28. Richard
  29.  
  30.  
  31. #include <stdio.h>
  32. #include <sys/types.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <termios.h>
  36.  
  37. int ttyfile;
  38. int errno;
  39.  
  40. void initial()
  41. {
  42.   struct termios bla;
  43.   int flags;
  44.  
  45.   flags = O_RDWR | O_NDELAY; 
  46.   ttyfile = open("/dev/ttya", flags);
  47.   if (ttyfile<0) {
  48.     fprintf(stderr,"error\n");
  49.     exit(1);
  50.   }
  51.   if (tcgetattr(ttyfile, &bla)) {
  52.     fprintf(stderr,"error\n");
  53.     exit(1);
  54.   }
  55.   bla.c_oflag = 0;
  56.   bla.c_iflag = 0;
  57.   bla.c_cflag = CS8 | CSTOPB | CLOCAL;
  58.   if (cfsetospeed(&bla,B2400)) {
  59.     fprintf(stderr,"error\n");
  60.     exit(1);
  61.   }
  62.   if (cfsetispeed(&bla,B2400)) {
  63.     fprintf(stderr,"error\n");
  64.     exit(1);
  65.   }
  66.  
  67.   if (tcsetattr(ttyfile, TCSANOW, &bla)) {
  68.     fprintf(stderr,"error\n");
  69.     exit(1);
  70.   }
  71. }
  72.  
  73.  
  74. int main()
  75.   char inp;
  76.   char outp;
  77.    
  78.  
  79.   initial();
  80.   inp = 193; 
  81.   if (write(ttyfile, &inp, 1) < 0) printf("Error writing\n");
  82.     else printf("Ready writing\n"); 
  83.   if (read(ttyfile, &outp,1) < 0)  /* Here the program hangs */
  84.      { printf("Error reading  ");
  85.        printf("%d\n", errno);
  86.      }
  87.    else printf("Ready reading\n");     
  88.   printf("Output :%s of %d of %o.\n", outp, outp, outp);
  89.   close(ttyfile);
  90. }
  91.  
  92. -- 
  93. Richard Braeken                   |                     Ura ni wa ura ga aru
  94. more info:                        |     The reverse sight has a reverse side 
  95.    finger exspectf@win.tue.nl     |                        (Japanese saying)
  96.